package com.yanfan.energy.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.yanfan.energy.domain.ConfigurationCop;
import com.yanfan.energy.mapper.ConfigurationCopMapper;
import com.yanfan.energy.service.ConfigurationCopService;
import com.yanfan.iot.cache.ITSLValueCache;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.model.ThingsModels.ValueItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * cop配置Service业务层处理
 *
 * @author kerwincui
 * @date 2025-07-15
 */
@Service
public class ConfigurationCopServiceImpl implements ConfigurationCopService {

    @Resource
    private ConfigurationCopMapper configurationCopMapper;
    @Resource
    private ITSLValueCache itslValueCache;

    @Override
    public ConfigurationCop selectConfigurationCopByCopId(Long copId) {
        return configurationCopMapper.selectConfigurationCopByCopId(copId);
    }

    @Override
    public List<ConfigurationCop> selectConfigurationCopList(ConfigurationCop configurationCop) {
        return configurationCopMapper.selectConfigurationCopList(configurationCop);
    }

    @Override
    public int insertConfigurationCop(ConfigurationCop configurationCop) {
        configurationCop.setCreateTime(new Date());
        return configurationCopMapper.insertConfigurationCop(configurationCop);
    }

    @Override
    public int updateConfigurationCop(ConfigurationCop configurationCop) {
        return configurationCopMapper.updateConfigurationCop(configurationCop);
    }

    @Override
    public int deleteConfigurationCopByCopId(Long copId) {
        return configurationCopMapper.deleteConfigurationCopByCopId(copId);
    }

    @Override
    public List<ConfigurationCop> selectConfigurationCopListBySerialNumber(String serialNumber) {
        return configurationCopMapper.selectConfigurationCopListBySerialNumber(serialNumber);
    }

    @Override
    public List<ValueItem> getValueItemListBySerialNumber(String serialNumber) {
        Device device = configurationCopMapper.selectDeviceBySerialNumber(serialNumber);
        List<ValueItem> list = itslValueCache.getCacheDeviceStatus(device.getProductId(), device.getSerialNumber());
        if (list != null && list.size() > 0) {
            return list;
        }
        return Collections.emptyList();
    }
}
